/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usuario.frames;

import usuario.control.UserFrameDadosSave;
import bibliinfo.FrameMain;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import usuario.model.Contato;
import usuario.model.Data;
import usuario.model.Endereco;
import usuario.model.Usuario;
import usuario.model.UsuarioDao;
import utilitarios.Validar;

/**
 *
 * @author Ar0n
 */
public class UserFrameMain extends javax.swing.JInternalFrame {

    /**
     * Creates new form UserFrameMain
     */
    public UserFrameMain() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jBPesquisa = new javax.swing.JButton();
        jTPesquisa = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTBNome = new javax.swing.JToggleButton();
        jTBRG = new javax.swing.JToggleButton();
        jTBSim = new javax.swing.JToggleButton();
        jTBNao = new javax.swing.JToggleButton();
        jPanel2 = new javax.swing.JPanel();
        jButton6 = new javax.swing.JButton();
        jBAbrir = new javax.swing.JButton();
        jScrollPane = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();

        jBPesquisa.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365775621_green-37.png"))); // NOI18N
        jBPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisaActionPerformed(evt);
            }
        });

        jTPesquisa.setPreferredSize(new java.awt.Dimension(0, 10));
        jTPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTPesquisaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTPesquisaKeyReleased(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Configurações de Pesquisa");
        jLabel5.setPreferredSize(new java.awt.Dimension(20, 15));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Pesquisar Excluidos:");
        jLabel6.setPreferredSize(new java.awt.Dimension(20, 15));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Pesquisar por:");
        jLabel7.setPreferredSize(new java.awt.Dimension(20, 15));

        jTBNome.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jTBNome.setSelected(true);
        jTBNome.setText("Nome");
        jTBNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBNomeActionPerformed(evt);
            }
        });

        jTBRG.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jTBRG.setText("RG");
        jTBRG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBRGActionPerformed(evt);
            }
        });

        jTBSim.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jTBSim.setText("Sim");
        jTBSim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBSimActionPerformed(evt);
            }
        });

        jTBNao.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jTBNao.setSelected(true);
        jTBNao.setText("Não");
        jTBNao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBNaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTBNao, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTBSim, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTBNome, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTBRG, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBPesquisa)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTBNome, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTBRG, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTBSim, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTBNao, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365307594_list-add-user.png"))); // NOI18N
        jButton6.setText("Novo Cadastro");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jBAbrir.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBAbrir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365307745_Open.png"))); // NOI18N
        jBAbrir.setText("Abrir");
        jBAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAbrirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBAbrir, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBAbrir, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35))
        );

        jScrollPane.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jTable.setModel(defaultTable);
        jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableMouseClicked(evt);
            }
        });
        jScrollPane.setViewportView(jTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        
        FrameMain.userFrameMain.setVisible(false);
        FrameMain.userFrameMain.setEstado(false);
                
        FrameMain.userFrameDados.setVisible(true);
        FrameMain.userFrameDados.setEstado(true);        
        FrameMain.userFrameDados.setComponente(1);        
        
        //formatando frame
        FrameMain.mapa.put("frame12", FrameMain.userFrameDados);
        FrameMain.formata(12);       
        
        //limpando habiblitando campos e os limpando caso hanjam restos
        UserFrameDadosSave limpa = new UserFrameDadosSave();
        limpa.limparCampos();
        FrameMain.userFrameDados.habilitaCampos();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jTBNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBNomeActionPerformed
        alterarButaoRG(1);
    }//GEN-LAST:event_jTBNomeActionPerformed

    private void jTBRGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBRGActionPerformed
        alterarButaoRG(2);
        jTPesquisaKeyPressed(null);
        jTPesquisaKeyReleased(null);
    }//GEN-LAST:event_jTBRGActionPerformed

    private void jTBNaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBNaoActionPerformed
        alterarButaoEx(1);
    }//GEN-LAST:event_jTBNaoActionPerformed

    private void jTBSimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBSimActionPerformed
        alterarButaoEx(2);
    }//GEN-LAST:event_jTBSimActionPerformed

    private void jTPesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTPesquisaKeyPressed
        if(jTBNome.isSelected() == true){
            Validar.validaTexto(jTPesquisa, 50);
        }else{
            Validar.validaNum(jTPesquisa, 11);
        }        
    }//GEN-LAST:event_jTPesquisaKeyPressed

    private void jTPesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTPesquisaKeyReleased
        if(jTBNome.isSelected() == true){
            Validar.validaTexto(jTPesquisa, 50);
            if(jTBNao.isSelected() == true){
                pesquisarNome(true);
            }else{
                pesquisarNome(false);
            }            
        }else{
            Validar.validaNum(jTPesquisa, 11);
            if(jTBNao.isSelected() == true){
                pesquisarRg(true);
            }else{
                pesquisarRg(false);
            }
            
        }
        
    }//GEN-LAST:event_jTPesquisaKeyReleased

    private void jBPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisaActionPerformed
        limpaTabela();
        if(jTBNome.isSelected() == true){
            //verificando se os usuário excluidos serão pesquisados ou não
            if(jTBNao.isSelected() == true){
                pesquisarNome(true);
            }else{
                pesquisarNome(false);
            }     
        }else{
            if(jTBNao.isSelected() == true){
                pesquisarRg(true);
            }else{
                pesquisarRg(false);
            }                 
        }   
    }//GEN-LAST:event_jBPesquisaActionPerformed

    private void jBAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAbrirActionPerformed

        try{
             if(rgSelecionado.equalsIgnoreCase("") == true){
                JOptionPane.showMessageDialog(FrameMain.userFrameMain, "Selecione um usuário na tabela", "", 1);
             }else{
                updateUser(rgSelecionado);
            }
        }catch(Exception e){
            System.out.println("null");
        }
       
    }//GEN-LAST:event_jBAbrirActionPerformed

    private void jTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableMouseClicked
        rgSelecionado = pegarUsuario(jTable);        
    }//GEN-LAST:event_jTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAbrir;
    public javax.swing.JButton jBPesquisa;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JToggleButton jTBNao;
    private javax.swing.JToggleButton jTBNome;
    private javax.swing.JToggleButton jTBRG;
    private javax.swing.JToggleButton jTBSim;
    private javax.swing.JTextField jTPesquisa;
    private javax.swing.JTable jTable;
    // End of variables declaration//GEN-END:variables
    //atributos
    public Object [] linhas;
    private static boolean estado = true;//true = visivel false = não visivel
    public DefaultTableModel defaultTable = new DefaultTableModel(null, new String [] {"Nome", "RG"});    
    public String rgSelecionado = "";
    public boolean acaoAbrir = false;
    //métodos    
    /**
     * Método criado para identicar o estado do frame
     */   
    public boolean getEstado(){
        return estado;
    }
    public void setEstado(boolean estado){
        this.estado = estado;
    }
    /**
     * Método criado para alterar o botões
     */
    
    public void alterarButaoEx(int i){    
        if(i == 1){
            jTBNao.setSelected(true);
            jTBSim.setSelected(false);
        }else if (i == 2){
            jTBNao.setSelected(false);
            jTBSim.setSelected(true);
        }        
    }
    
    public void alterarButaoRG(int i){
        if(i == 1){
            jTBNome.setSelected(true);
            jTBRG.setSelected(false);
        }else if(i == 2){
            jTBNome.setSelected(false);
            jTBRG.setSelected(true);
        }
    }
    
    //método para limpar a tabela
    public void limpaTabela(){
        while(defaultTable.getRowCount() > 0){
            defaultTable.removeRow(0);
        }
    }
    /**
     Método que valida os campos para o nome ser pesquisado
     */
    public void pesquisarNome(boolean ativo){
        UsuarioDao userDao = new UsuarioDao();       
        limpaTabela();        
        if(jTPesquisa.getText().equalsIgnoreCase("") == true){            
            List<Usuario> listaUser = userDao.readA();
            listaUser = filtra(listaUser, ativo);
            //Object [] linhas;
            for(int i = 0; i < listaUser.size(); i++){            
                linhas = new Object []{listaUser.get(i).getNome(), String.valueOf(listaUser.get(i).getRg())};                
                defaultTable.addRow(linhas);            
            }        
        }else{
            String pesquisar = jTPesquisa.getText();            
            List<Usuario> listaUser= userDao.read("%"+pesquisar+"%", 1);
            listaUser = filtra(listaUser, ativo);
            //Object [] linhas;
            for(int i = 0; i < listaUser.size(); i++){            
                linhas = new Object []{listaUser.get(i).getNome(), String.valueOf(listaUser.get(i).getRg())};
                defaultTable.addRow(linhas);               
            }
        }
    }
    /**
     Método que valida os campos para o rg ser pesquisado
     */
    public void pesquisarRg(boolean ativo){
        UsuarioDao userDao = new UsuarioDao();
        limpaTabela();
        String rgTexto = FrameMain.userFrameMain.jTPesquisa.getText();
        if(rgTexto.equalsIgnoreCase("")){
            List<Usuario> listaUser = userDao.readA();
            listaUser = filtra(listaUser, ativo);            
            for(int i = 0; i < listaUser.size(); i++){            
                linhas = new Object []{listaUser.get(i).getNome(), String.valueOf(listaUser.get(i).getRg())};                
                defaultTable.addRow(linhas);            
            }    
        }else{            
            List<Usuario> listaUser = userDao.read("%"+rgTexto+"%", 2);
            listaUser = filtra(listaUser, ativo);            
            for(int i = 0; i < listaUser.size(); i++){            
                linhas = new Object []{listaUser.get(i).getNome(), String.valueOf(listaUser.get(i).getRg())};
                defaultTable.addRow(linhas);               
            }
        }
    }
    
    /*Método usado para filtrar usuários excluidos ou não excluidos*/
    public List filtra(List<Usuario> listaUser, boolean  ativo){
        List<Usuario> listaUserRetorno = new ArrayList<>();
        if(ativo == true){
            for(Usuario user : listaUser){
            if(user.getAtivo() == true){
                listaUserRetorno.add(user);
            }
        }
        }else{
            for(Usuario user : listaUser){
                if(user.getAtivo() == false){
                    listaUserRetorno.add(user);
                }
            }
        }
       return listaUserRetorno; 
    }
    /**
     Metodo que ira pegar o rg da tabela filtrar o banco pegar todos os dados
     * e coloca-los dentro da minha janela de dados
     */
    public String pegarUsuario(JTable tabela){
        String a = "";         
        int i = tabela.getSelectedRow();
        a = String.valueOf(tabela.getValueAt(i, 1));                    
        return a;
    }
    /**
     Método que abri os dados para edição
     */
    public void updateUser(String rg){
       UsuarioDao dao = new UsuarioDao();              
       List<Usuario> listaUser = dao.readA();
       List<Endereco> listaEnd = dao.readB();
       List<Contato> listaCont = dao.readC();
       List<Data> listaData = dao.readD();
       
       for(Usuario user : listaUser){
           if(rg.equalsIgnoreCase(String.valueOf(user.getRg()))){
               FrameMain.userFrameMain.setVisible(false);
               FrameMain.userFrameMain.setEstado(false);
               
               //FrameMain.userFrameDados.mapa<"frame12",>               
               FrameMain.userFrameDados.setVisible(true);
               FrameMain.userFrameDados.setEstado(true);  
               FrameMain.userFrameDados.setComponente(2);
               FrameMain.formata(12);
               FrameMain.userFrameDados.desabilitaCampos();
               
               FrameMain.userFrameDados.jTRG.setText(String.valueOf(user.getRg()));
               /**
                Pegando RG do usuário para ser usado na linha abaixo para na parte de edição de dados
                * como o rg que foi usado antes de ele ser trocado.
                */
               FrameMain.userFrameDados.setRgAntigo(user.getRg());               
               FrameMain.userFrameDados.jTNome.setText(user.getNome());
               if(user.getSexo().equalsIgnoreCase("f")){
                   FrameMain.userFrameDados.jCBSexo.setSelectedIndex(1);
               }               
               FrameMain.userFrameAdd.alteraRadio(user.getSuspenso());              
               FrameMain.userFrameAdd.setExcluir(user.getAtivo());               
               FrameMain.userFrameDados.jTAObs.setText(user.getObservacoes());
               
               for(Endereco endereco : listaEnd){
                   if(rg.equalsIgnoreCase(String.valueOf(endereco.getRg()))){
                       //setando comboBox do estado
                       if(endereco.getEstado().equalsIgnoreCase("AC")){
                            FrameMain.userFrameDados.jCBEstado.setSelectedIndex(0);
                       }else if(endereco.getEstado().equalsIgnoreCase("AL")){
                            FrameMain.userFrameDados.jCBEstado.setSelectedIndex(1);
                       }else if(endereco.getEstado().equalsIgnoreCase("AP")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(2);
                       }else if(endereco.getEstado().equalsIgnoreCase("AM")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(3);
                       }else if(endereco.getEstado().equalsIgnoreCase("BA")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(4);
                       }else if(endereco.getEstado().equalsIgnoreCase("CE")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(5);
                       }else if(endereco.getEstado().equalsIgnoreCase("DF")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(6);
                       }else if(endereco.getEstado().equalsIgnoreCase("ES")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(7);
                       }else if(endereco.getEstado().equalsIgnoreCase("GO")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(8);
                       }else if(endereco.getEstado().equalsIgnoreCase("MA")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(9);
                       }else if(endereco.getEstado().equalsIgnoreCase("MT")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(10);
                       }else if(endereco.getEstado().equalsIgnoreCase("MS")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(11);
                       }else if(endereco.getEstado().equalsIgnoreCase("MG")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(12);
                       }else if(endereco.getEstado().equalsIgnoreCase("PA")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(13);
                       }else if(endereco.getEstado().equalsIgnoreCase("PB")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(14);
                       }else if(endereco.getEstado().equalsIgnoreCase("PR")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(15);
                       }else if(endereco.getEstado().equalsIgnoreCase("PE")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(16);
                       }else if(endereco.getEstado().equalsIgnoreCase("PI")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(17);
                       }else if(endereco.getEstado().equalsIgnoreCase("RJ")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(18);
                       }else if(endereco.getEstado().equalsIgnoreCase("RN")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(18);
                       }else if(endereco.getEstado().equalsIgnoreCase("RS")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(20);
                       }else if(endereco.getEstado().equalsIgnoreCase("RO")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(21);
                       }else if(endereco.getEstado().equalsIgnoreCase("RR")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(22);
                       }else if(endereco.getEstado().equalsIgnoreCase("SC")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(23);
                       }else if(endereco.getEstado().equalsIgnoreCase("SP")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(24);
                       }else if(endereco.getEstado().equalsIgnoreCase("SE")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(25);
                       }else if(endereco.getEstado().equalsIgnoreCase("TO")){
                             FrameMain.userFrameDados.jCBEstado.setSelectedIndex(26);
                       }
                       
                       FrameMain.userFrameDados.jTCidade.setText(endereco.getCidade());                                       
                       FrameMain.userFrameDados.jTCep.setText(endereco.getCep());                                             
                       FrameMain.userFrameDados.jTBairro.setText(endereco.getBairro());
                       FrameMain.userFrameDados.jTRua.setText(endereco.getRua());
                       FrameMain.userFrameDados.jTNumCasa.setText(endereco.getNumCasa());
                       
                       for(Contato contato : listaCont){
                           if(rg.equalsIgnoreCase(String.valueOf(contato.getRg()))){
                               FrameMain.userFrameDados.jTTel_1.setText(contato.getTel_1());
                               FrameMain.userFrameDados.jTTel_2.setText(contato.getTel_2());
                               FrameMain.userFrameDados.jTTel_3.setText(contato.getTel_3());
                               FrameMain.userFrameDados.jTTel_4.setText(contato.getTel_4());
                               FrameMain.userFrameDados.jTEmail_1.setText(contato.getEmail_1());
                               FrameMain.userFrameDados.jTEmail_2.setText(contato.getEmail_2());                               
                           }
                           for(Data data : listaData){
                               if(rg.equalsIgnoreCase(String.valueOf(data.getRg()))){                            
                                   
                                   data.calendar.setTime(data.getDataNasc());
                                   FrameMain.userFrameDados.jTDia.setText(String.valueOf(data.calendar
                                           .get(Calendar.DAY_OF_MONTH)));                                          
                                   FrameMain.userFrameDados.jTMes.setText(String.valueOf(data.calendar
                                           .get(Calendar.MONTH) + 1));
                                   FrameMain.userFrameDados.jTAno.setText(String.valueOf(data.calendar
                                           .get(Calendar.YEAR)));
                                   
                                   data.calendar.setTime(data.getDataCad());
                                   FrameMain.userFrameAdd.jTDataCadDia.setText(String.valueOf(data.calendar
                                           .get(Calendar.DAY_OF_MONTH)));
                                   FrameMain.userFrameAdd.jTDataCadMes.setText(String.valueOf(data.calendar
                                           .get(Calendar.MONTH) + 1));                                   
                                   FrameMain.userFrameAdd.jTDataCadAno.setText(String.valueOf(data.calendar
                                           .get(Calendar.YEAR)));
                                   
                                   data.calendar.setTime(data.getDataInicioMulta());
                                   if(data.calendar.get(Calendar.YEAR) != 0001){
                                       FrameMain.userFrameAdd.jTDataInicioDia.setText(String.valueOf(data.calendar
                                               .get(Calendar.DAY_OF_MONTH)));
                                       FrameMain.userFrameAdd.jTDataInicioMes.setText(String.valueOf(data.calendar
                                               .get(Calendar.MONTH) +1));
                                       FrameMain.userFrameAdd.jTDataInicioAno.setText(String.valueOf(data.calendar
                                               .get(Calendar.YEAR)));
                                       
                                       data.calendar.setTime(data.getDataFimMulta());
                                       FrameMain.userFrameAdd.jTDataFimDia.setText(String.valueOf(data.calendar
                                               .get(Calendar.DAY_OF_MONTH)));                                       
                                       FrameMain.userFrameAdd.jTDataFimMes.setText(String.valueOf(data.calendar
                                               .get(Calendar.MONTH) +1));
                                       FrameMain.userFrameAdd.jTDataFimAno.setText(String.valueOf(data.calendar
                                               .get(Calendar.YEAR)));
                                       
                                   }
                               }                              
                           }
                       }
                   }
               }
           }
       }
    }
}
